<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\DaibanService;
use Illuminate\Http\Request;

class DaibanController extends BaseController
{
    //代办

    public function __construct(DaibanService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption()
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //医生列表
    public function doctorList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['doc_name'] = $request->post('doc_name', '');
        $pas['is_sort'] = $request->post('is_sort', 0); //1升序，2降序
        $res = $this->db->doctorList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //医生详情
    public function doctorOne(Request $request)
    {
        $did = $request->post('did', 0);
        $res = $this->db->doctorOne($did);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //医院模糊搜索
    public function yiyuanByKey(Request $request)
    {
        $key = $request->post('key', '');
        $res = $this->db->yiyuanByKey($key);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['type'] = $request->post('type', 0); //代办服务ID
        $pas['did'] = $request->post('doc_uid', 0);
        $pas['depart_name'] = $request->post('depart_name', ''); //科室
        $pas['alias_name'] = $request->post('alias_name', ''); //号别
        $pas['price'] = $request->post('price', 0); //价格
        $pas['yytime'] = $request->post('yytime', ''); //预约时间
        $pas['yiyuan'] = $request->post('yiyuan', ''); //医院
        $pas['yy_map'] = $request->post('yy_map', ''); //预约时经纬度
        $pas['remark'] = $request->post('remark', '');
        $attrList = $request->post('attrList', ''); //属性JSON字符串
        $pas['price'] = $pas['price'] ? $pas['price'] : 0;
        if (mb_substr($pas['yytime'], -6) == '(已有预约)') {
            $pas['yytime'] = mb_substr($pas['yytime'], 0, 10);
        }
    /*    if (!$pas['type']) {
            return $this->toJson(-2, '服务类型必须的');
        }*/
        if (!$pas['yytime']) {
            return $this->toJson(-2, '预约时间必须的');
        }
        $res = $this->db->add($pas, $attrList);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0 , '', $res['data']);
    }

    /**
     * 患者预约
     */
    public function patYuyue(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['type'] = $request->post('cate_id', 0);
        $pas['did'] = $request->post('doc_uid', 0);
        $pas['price'] = $request->post('price', 0);
        $pas['keshi'] = $request->post('keshi', 0);
        $pas['alias'] = $request->post('alias', 0);
        $pas['yytime'] = $request->post('yytime', '');
        $yiyuan = $request->post('yiyuan', '');
        $pas['yy_map'] = $request->post('yy_map', ''); //预约时经纬度
        $pas['remark'] = $request->post('remark', '');
        if (!$pas['type']) {
            return $this->toJson(-2, '类型必须的');
        }
        if (!$pas['did']) {
            return $this->toJson(-2, '医生必须的');
        }
        if (!$pas['yytime']) {
            return $this->toJson(-2, '预约时间必须的');
        }
        $yyid = $this->db->hospitalIdByName($yiyuan);
        if (!$yyid) {
            return $this->toJson(-2, '预约医院必须的');
        }
        $pas['yiyuan'] = $yyid;
        $res = $this->db->patYuyue($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 详情
     */
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 医生接单
     */
    public function doctorRob(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->doctorRob($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}